package com.onluy.frameworkcloud.tools;


import com.onluy.frameworkcloud.tools.utils.StringUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtil {

    public DateUtil() {
    }

    public static Date now() {
        return new Date();
    }

    public static String now(String pattern) {
        if (pattern == null || "".equals(pattern.trim()))
            pattern = "yyyyMMddHHmmss";
        return formatDate(now(), pattern);
    }

    public static String formatDate(Date date, String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }

    public static Date parseDate(String date, String pattern) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Date d = format.parse(date);
        return d;
    }

    public static String getFormattedDateNew(String strDate, String originalPattern, String formatedPattern) {
        if (StringUtil.isBlank(strDate))
            return "";
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime((new SimpleDateFormat(originalPattern)).parse(strDate));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return (new SimpleDateFormat(formatedPattern)).format(cal.getTime());
    }

    /**
     * @deprecated Method getFormattedDate is deprecated
     */

    public static String getFormattedDate(String strDate, String strFormatTo) {
        String formatStr = "yyyyMMdd";
        if (strDate == null || strDate.trim().equals(""))
            return "";
        switch (strDate.trim().length()) {
            case 6: // '\006'
                if (strDate.substring(0, 1).equals("0") || strDate.substring(0, 1).equals("1"))
                    formatStr = "yyMMdd";
                else
                    formatStr = "yyyyMM";
                break;

            case 8: // '\b'
                formatStr = "yyyyMMdd";
                break;

            case 10: // '\n'
                if (strDate.indexOf("-") == -1)
                    formatStr = "yyyy/MM/dd";
                else
                    formatStr = "yyyy-MM-dd";
                break;

            case 11: // '\013'
                if (strDate.getBytes().length == 14)
                    formatStr = "yyyy年MM月dd日";
                else
                    return "";
                // fall through

            case 14: // '\016'
                formatStr = "yyyyMMddHHmmss";
                break;

            case 19: // '\023'
                if (strDate.indexOf("-") == -1)
                    formatStr = "yyyy/MM/dd HH:mm:ss";
                else
                    formatStr = "yyyy-MM-dd HH:mm:ss";
                break;

            case 7: // '\007'
            case 9: // '\t'
            case 12: // '\f'
            case 13: // '\r'
            case 15: // '\017'
            case 16: // '\020'
            case 17: // '\021'
            case 18: // '\022'
            default:
                return strDate.trim();
        }
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(formatter.parse(strDate));
            formatter = new SimpleDateFormat(strFormatTo);
            return formatter.format(calendar.getTime());
        } catch (Exception e) {
            System.out.println((new StringBuilder("转换日期字符串格式时出错;")).append(e.getMessage()).toString());
        }
        return "";
    }

    public static void main(String args[]) {
        System.out.println(getFormattedDateNew("2016-08-13 20:04", "yyyy-MM-dd hh:mm", "yyyyMMddHHmmss"));
    }

    public static final String DATE_FORMAT_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static final String DATE_FORMAT_YYYYMMDDHHMMSS_ORACLE = "yyyymmddHH24miss";
    public static final String DATE_FORMAT_YYYYMMDDHHMM = "yyyyMMddHHmm";
    public static final String DATE_FORMAT_YYYYMMDDHH = "yyyyMMddHH";
    public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
    public static final String DATE_FORMAT_YYYYMM = "yyyyMM";
    public static final String DATE_FORMAT_YYYY = "yyyy";
    public static final String DATE_FORMAT_EN_A_YYYYMMDDHHMMSS = "yyyy/MM/dd HH:mm:ss";
    public static final String DATE_FORMAT_EN_A_YYYYMMDDHHMM = "yyyy/MM/dd HH:mm";
    public static final String DATE_FORMAT_EN_A_YYYYMMDDHH = "yyyy/MM/dd HH";
    public static final String DATE_FORMAT_EN_A_YYYYMMDD = "yyyy/MM/dd";
    public static final String DATE_FORMAT_EN_A_YYYYMM = "yyyy/MM";
    public static final String DATE_FORMAT_EN_A_YYYY = "yyyy";
    public static final String DATE_FORMAT_EN_B_YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_EN_B_YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT_EN_B_YYYYMMDDHH = "yyyy-MM-dd HH";
    public static final String DATE_FORMAT_EN_B_YYYYMMDD = "yyyy-MM-dd";
    public static final String DATE_FORMAT_EN_B_YYYYMM = "yyyy-MM";
    public static final String DATE_FORMAT_EN_B_YYYY = "yyyy";
    public static final String DATE_FORMAT_CN_YYYYMMDDHHMMSS = "yyyy'年'MM'月'dd'日' HH'时'mm'分'ss'秒'";
    public static final String DATE_FORMAT_CN_YYYYMMDDHHMM = "yyyy'年'MM'月'dd'日' HH'时'mm'分'";
    public static final String DATE_FORMAT_CN_YYYYMMDDHH = "yyyy'年'MM'月'dd'日' HH'时'";
    public static final String DATE_FORMAT_CN_YYYYMMDD = "yyyy'年'MM'月'dd'日'";
    public static final String DATE_FORMAT_CN_YYYYMM = "yyyy'年'MM'月'";
    public static final String DATE_FORMAT_CN_YYYY = "yyyy'年'";
    public static String MATCH_YEAR_MONTH_DAY = "(19|20)\\d\\d([- /.]?)(0[1-9]|1[012])\\2(0[1-9]|[12][0-9]|3[01])";
    public static String MATCH_YEAR_MONTH_DAY_YYYYMMDD = "\\d\\d\\d\\d(0[1-9]|1[012])\\2(0[1-9]|[12][0-9]|3[01])";
    public static String MATCH_YEAR_MONTH_DAY_YYYYMM = "\\d\\d\\d\\d(0[1-9]|1[012])";
    public static SimpleDateFormat defaultSimpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");

}